﻿using System;
using UnityEngine;

namespace RootMotion.FinalIK
{
	// Token: 0x02000164 RID: 356
	[Serializable]
	public class IKMapping
	{
		// Token: 0x06000612 RID: 1554 RVA: 0x0001D6E4 File Offset: 0x0001BAE4
		public virtual bool IsValid(IKSolver solver, ref string message)
		{
			return true;
		}

		// Token: 0x06000613 RID: 1555 RVA: 0x0001D6E7 File Offset: 0x0001BAE7
		public virtual void Initiate(IKSolverFullBody solver)
		{
		}

		// Token: 0x06000614 RID: 1556 RVA: 0x0001D6EC File Offset: 0x0001BAEC
		protected bool BoneIsValid(Transform bone, IKSolver solver, ref string message, Warning.Logger logger = null)
		{
			if (bone == null)
			{
				message = "IKMappingLimb contains a null reference.";
				if (logger != null)
				{
					logger(message);
				}
				return false;
			}
			if (solver.GetPoint(bone) == null)
			{
				message = "IKMappingLimb is referencing to a bone '" + bone.name + "' that does not excist in the Node Chain.";
				if (logger != null)
				{
					logger(message);
				}
				return false;
			}
			return true;
		}

		// Token: 0x06000615 RID: 1557 RVA: 0x0001D754 File Offset: 0x0001BB54
		protected Vector3 SolveFABRIKJoint(Vector3 pos1, Vector3 pos2, float length)
		{
			return pos2 + (pos1 - pos2).normalized * length;
		}

		// Token: 0x02000165 RID: 357
		[Serializable]
		public class BoneMap
		{
			// Token: 0x06000617 RID: 1559 RVA: 0x0001D7BC File Offset: 0x0001BBBC
			public void Initiate(Transform transform, IKSolverFullBody solver)
			{
				this.transform = transform;
				solver.GetChainAndNodeIndexes(transform, out this.chainIndex, out this.nodeIndex);
			}

			// Token: 0x17000050 RID: 80
			// (get) Token: 0x06000618 RID: 1560 RVA: 0x0001D7D8 File Offset: 0x0001BBD8
			public Vector3 swingDirection
			{
				get
				{
					return this.transform.rotation * this.localSwingAxis;
				}
			}

			// Token: 0x06000619 RID: 1561 RVA: 0x0001D7F0 File Offset: 0x0001BBF0
			public void StoreDefaultLocalState()
			{
				this.defaultLocalPosition = this.transform.localPosition;
				this.defaultLocalRotation = this.transform.localRotation;
			}

			// Token: 0x0600061A RID: 1562 RVA: 0x0001D814 File Offset: 0x0001BC14
			public void FixTransform(bool position)
			{
				if (position)
				{
					this.transform.localPosition = this.defaultLocalPosition;
				}
				this.transform.localRotation = this.defaultLocalRotation;
			}

			// Token: 0x17000051 RID: 81
			// (get) Token: 0x0600061B RID: 1563 RVA: 0x0001D83E File Offset: 0x0001BC3E
			public bool isNodeBone
			{
				get
				{
					return this.nodeIndex != -1;
				}
			}

			// Token: 0x0600061C RID: 1564 RVA: 0x0001D84C File Offset: 0x0001BC4C
			public void SetLength(IKMapping.BoneMap nextBone)
			{
				this.length = Vector3.Distance(this.transform.position, nextBone.transform.position);
			}

			// Token: 0x0600061D RID: 1565 RVA: 0x0001D86F File Offset: 0x0001BC6F
			public void SetLocalSwingAxis(IKMapping.BoneMap swingTarget)
			{
				this.SetLocalSwingAxis(swingTarget, this);
			}

			// Token: 0x0600061E RID: 1566 RVA: 0x0001D879 File Offset: 0x0001BC79
			public void SetLocalSwingAxis(IKMapping.BoneMap bone1, IKMapping.BoneMap bone2)
			{
				this.localSwingAxis = Quaternion.Inverse(this.transform.rotation) * (bone1.transform.position - bone2.transform.position);
			}

			// Token: 0x0600061F RID: 1567 RVA: 0x0001D8B1 File Offset: 0x0001BCB1
			public void SetLocalTwistAxis(Vector3 twistDirection, Vector3 normalDirection)
			{
				Vector3.OrthoNormalize(ref normalDirection, ref twistDirection);
				this.localTwistAxis = Quaternion.Inverse(this.transform.rotation) * twistDirection;
			}

			// Token: 0x06000620 RID: 1568 RVA: 0x0001D8D8 File Offset: 0x0001BCD8
			public void SetPlane(IKSolverFullBody solver, Transform planeBone1, Transform planeBone2, Transform planeBone3)
			{
				this.planeBone1 = planeBone1;
				this.planeBone2 = planeBone2;
				this.planeBone3 = planeBone3;
				solver.GetChainAndNodeIndexes(planeBone1, out this.plane1ChainIndex, out this.plane1NodeIndex);
				solver.GetChainAndNodeIndexes(planeBone2, out this.plane2ChainIndex, out this.plane2NodeIndex);
				solver.GetChainAndNodeIndexes(planeBone3, out this.plane3ChainIndex, out this.plane3NodeIndex);
				this.UpdatePlane(true, true);
			}

			// Token: 0x06000621 RID: 1569 RVA: 0x0001D940 File Offset: 0x0001BD40
			public void UpdatePlane(bool rotation, bool position)
			{
				Quaternion lastAnimatedTargetRotation = this.lastAnimatedTargetRotation;
				if (rotation)
				{
					this.defaultLocalTargetRotation = QuaTools.RotationToLocalSpace(this.transform.rotation, lastAnimatedTargetRotation);
				}
				if (position)
				{
					this.planePosition = Quaternion.Inverse(lastAnimatedTargetRotation) * (this.transform.position - this.planeBone1.position);
				}
			}

			// Token: 0x06000622 RID: 1570 RVA: 0x0001D9A3 File Offset: 0x0001BDA3
			public void SetIKPosition()
			{
				this.ikPosition = this.transform.position;
			}

			// Token: 0x06000623 RID: 1571 RVA: 0x0001D9B6 File Offset: 0x0001BDB6
			public void MaintainRotation()
			{
				this.maintainRotation = this.transform.rotation;
			}

			// Token: 0x06000624 RID: 1572 RVA: 0x0001D9C9 File Offset: 0x0001BDC9
			public void SetToIKPosition()
			{
				this.transform.position = this.ikPosition;
			}

			// Token: 0x06000625 RID: 1573 RVA: 0x0001D9DC File Offset: 0x0001BDDC
			public void FixToNode(IKSolverFullBody solver, float weight, IKSolver.Node fixNode = null)
			{
				if (fixNode == null)
				{
					fixNode = solver.GetNode(this.chainIndex, this.nodeIndex);
				}
				if (weight >= 1f)
				{
					this.transform.position = fixNode.solverPosition;
					return;
				}
				this.transform.position = Vector3.Lerp(this.transform.position, fixNode.solverPosition, weight);
			}

			// Token: 0x06000626 RID: 1574 RVA: 0x0001DA42 File Offset: 0x0001BE42
			public Vector3 GetPlanePosition(IKSolverFullBody solver)
			{
				return solver.GetNode(this.plane1ChainIndex, this.plane1NodeIndex).solverPosition + this.GetTargetRotation(solver) * this.planePosition;
			}

			// Token: 0x06000627 RID: 1575 RVA: 0x0001DA72 File Offset: 0x0001BE72
			public void PositionToPlane(IKSolverFullBody solver)
			{
				this.transform.position = this.GetPlanePosition(solver);
			}

			// Token: 0x06000628 RID: 1576 RVA: 0x0001DA88 File Offset: 0x0001BE88
			public void RotateToPlane(IKSolverFullBody solver, float weight)
			{
				Quaternion quaternion = this.GetTargetRotation(solver) * this.defaultLocalTargetRotation;
				if (weight >= 1f)
				{
					this.transform.rotation = quaternion;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, quaternion, weight);
			}

			// Token: 0x06000629 RID: 1577 RVA: 0x0001DADD File Offset: 0x0001BEDD
			public void Swing(Vector3 swingTarget, float weight)
			{
				this.Swing(swingTarget, this.transform.position, weight);
			}

			// Token: 0x0600062A RID: 1578 RVA: 0x0001DAF4 File Offset: 0x0001BEF4
			public void Swing(Vector3 pos1, Vector3 pos2, float weight)
			{
				Quaternion quaternion = Quaternion.FromToRotation(this.transform.rotation * this.localSwingAxis, pos1 - pos2) * this.transform.rotation;
				if (weight >= 1f)
				{
					this.transform.rotation = quaternion;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, quaternion, weight);
			}

			// Token: 0x0600062B RID: 1579 RVA: 0x0001DB6C File Offset: 0x0001BF6C
			public void Twist(Vector3 twistDirection, Vector3 normalDirection, float weight)
			{
				Vector3.OrthoNormalize(ref normalDirection, ref twistDirection);
				Quaternion quaternion = Quaternion.FromToRotation(this.transform.rotation * this.localTwistAxis, twistDirection) * this.transform.rotation;
				if (weight >= 1f)
				{
					this.transform.rotation = quaternion;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, quaternion, weight);
			}

			// Token: 0x0600062C RID: 1580 RVA: 0x0001DBE4 File Offset: 0x0001BFE4
			public void RotateToMaintain(float weight)
			{
				if (weight <= 0f)
				{
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, this.maintainRotation, weight);
			}

			// Token: 0x0600062D RID: 1581 RVA: 0x0001DC14 File Offset: 0x0001C014
			public void RotateToEffector(IKSolverFullBody solver, float weight)
			{
				if (!this.isNodeBone)
				{
					return;
				}
				float num = weight * solver.GetNode(this.chainIndex, this.nodeIndex).effectorRotationWeight;
				if (num <= 0f)
				{
					return;
				}
				if (num >= 1f)
				{
					this.transform.rotation = solver.GetNode(this.chainIndex, this.nodeIndex).solverRotation;
					return;
				}
				this.transform.rotation = Quaternion.Lerp(this.transform.rotation, solver.GetNode(this.chainIndex, this.nodeIndex).solverRotation, num);
			}

			// Token: 0x0600062E RID: 1582 RVA: 0x0001DCB4 File Offset: 0x0001C0B4
			private Quaternion GetTargetRotation(IKSolverFullBody solver)
			{
				Vector3 solverPosition = solver.GetNode(this.plane1ChainIndex, this.plane1NodeIndex).solverPosition;
				Vector3 solverPosition2 = solver.GetNode(this.plane2ChainIndex, this.plane2NodeIndex).solverPosition;
				Vector3 solverPosition3 = solver.GetNode(this.plane3ChainIndex, this.plane3NodeIndex).solverPosition;
				if (solverPosition == solverPosition3)
				{
					return Quaternion.identity;
				}
				return Quaternion.LookRotation(solverPosition2 - solverPosition, solverPosition3 - solverPosition);
			}

			// Token: 0x17000052 RID: 82
			// (get) Token: 0x0600062F RID: 1583 RVA: 0x0001DD30 File Offset: 0x0001C130
			private Quaternion lastAnimatedTargetRotation
			{
				get
				{
					if (this.planeBone1.position == this.planeBone3.position)
					{
						return Quaternion.identity;
					}
					return Quaternion.LookRotation(this.planeBone2.position - this.planeBone1.position, this.planeBone3.position - this.planeBone1.position);
				}
			}

			// Token: 0x04000352 RID: 850
			public Transform transform;

			// Token: 0x04000353 RID: 851
			public int chainIndex = -1;

			// Token: 0x04000354 RID: 852
			public int nodeIndex = -1;

			// Token: 0x04000355 RID: 853
			public Vector3 defaultLocalPosition;

			// Token: 0x04000356 RID: 854
			public Quaternion defaultLocalRotation;

			// Token: 0x04000357 RID: 855
			public Vector3 localSwingAxis;

			// Token: 0x04000358 RID: 856
			public Vector3 localTwistAxis;

			// Token: 0x04000359 RID: 857
			public Vector3 planePosition;

			// Token: 0x0400035A RID: 858
			public Vector3 ikPosition;

			// Token: 0x0400035B RID: 859
			public Quaternion defaultLocalTargetRotation;

			// Token: 0x0400035C RID: 860
			private Quaternion maintainRotation;

			// Token: 0x0400035D RID: 861
			public float length;

			// Token: 0x0400035E RID: 862
			public Quaternion animatedRotation;

			// Token: 0x0400035F RID: 863
			private Transform planeBone1;

			// Token: 0x04000360 RID: 864
			private Transform planeBone2;

			// Token: 0x04000361 RID: 865
			private Transform planeBone3;

			// Token: 0x04000362 RID: 866
			private int plane1ChainIndex = -1;

			// Token: 0x04000363 RID: 867
			private int plane1NodeIndex = -1;

			// Token: 0x04000364 RID: 868
			private int plane2ChainIndex = -1;

			// Token: 0x04000365 RID: 869
			private int plane2NodeIndex = -1;

			// Token: 0x04000366 RID: 870
			private int plane3ChainIndex = -1;

			// Token: 0x04000367 RID: 871
			private int plane3NodeIndex = -1;
		}
	}
}
